package com.zeedev.flyingsaucer.beers.activities.beerdialog;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.*;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.name.Named;
import com.zeedev.flyingsaucer.ufoclub.R;
import mobi.zeedev.flyingsaucer.common.DataModule.FlyingSaucerConv.Beers;
import mobi.zeedev.flyingsaucer.common.DataModule.MetaBeer;
import mobi.zeedev.flyingsaucer.common.DataModule.Util;
import roboguice.activity.RoboListActivity;
import roboguice.inject.InjectExtra;
import roboguice.inject.InjectResource;
import roboguice.inject.InjectView;

public class BeerDialog2 extends RoboListActivity {

    @InjectExtra(MetaBeer.BEER_ID)
    Integer beerId;

//    @InjectView(R.id.beer_dialog_4_parent)
//    View beerDialogParent;

    @InjectView(R.id.beer_dialog_dismiss)
    ImageView beerDialogDismissButton;

//    @Inject
//    BeerDialogModel model;

    @Inject
    BeerDialogController beerDialogController;

    private ProgressDialog progress;

    @Inject
    @Named("dialogCursor")
    Provider<Cursor> mCursorProvider;

    @InjectResource(R.drawable.can)
    Drawable canImage;

    @InjectResource(R.drawable.draught)
    Drawable draughtImage;

    @InjectResource(R.drawable.bottle)
    Drawable bottleImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
                WindowManager.LayoutParams.FLAG_BLUR_BEHIND);


//        this.setContentView(R.layout.beer_dialog_4);
        this.setContentView(R.layout.beer_dialog_5);


/*        if (beerId == null || beerId == -1) {
            Ln.w("The beer dialog should not be called with a beerId.");
            this.setResult(Activity.RESULT_CANCELED);
            this.finish();
            return;
        }*/

        beerDialogDismissButton.setOnClickListener(dismissButtonListener);

        SimpleCursorAdapter sca = new SimpleCursorAdapter(
                this,
                R.layout.beer_dialog_4_listview,
                mCursorProvider.get(),
                Util.asAr(
                        Beers.STYLE_NAME,
                        Beers.BREWER_NAME,
                        Beers.DESCRIPTION,
                        Beers.COUNTRY_NAME,
                        Beers.NAME,
                        Beers.CAN,
                        Beers.NAME,
                        Beers.DESCRIPTION
                ),
                Util.asIntArray(
                        R.id.beer_dialog_style,
                        R.id.beer_dialog_brewer,
                        R.id.beer_dialog_description,
                        R.id.beer_dialog_country,
                        R.id.textBeerName,
                        R.id.textBeerContainer,
                        R.id.beer_dialog_4_listview_progress_indicator,
                        R.id.beer_dialog_4_listview_description_container
                )
        );

        this.setListAdapter(sca);

        sca.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
            @Override
            public boolean setViewValue(View view, Cursor cursor, int columnIndex) {

                if (cursor == null || view == null) {
                    return false;
                }

                if (view.getClass() == ProgressBar.class) {
                    ProgressBar pb = (ProgressBar) view;
                    String desc = cursor.getString(cursor.getColumnIndex(Beers.DESCRIPTION));
                    if (TextUtils.isEmpty(desc)) {
                        pb.setIndeterminate(true);
                        pb.setVisibility(View.VISIBLE);
                    } else {
                        pb.setVisibility(View.GONE);
                    }
                    return true;
                }

                if (view.getClass() == ViewFlipper.class) {
                    ViewFlipper viewFlipper = (ViewFlipper) view;
                    String desc = cursor.getString(cursor.getColumnIndex(Beers.DESCRIPTION));
                    if (TextUtils.isEmpty(desc)) {
                        viewFlipper.setDisplayedChild(0);
                    } else {
                        viewFlipper.setDisplayedChild(1);
                    }

                    return true;
                }

                if (cursor.getColumnName(columnIndex).equals(Beers.DESCRIPTION)) {
                    String originalDescription = cursor.getString(cursor.getColumnIndex(Beers.DESCRIPTION));

                    TextView textView = (TextView) view;

                    textView.setMovementMethod(new ScrollingMovementMethod());
                    textView.setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET);

                    if (!TextUtils.isEmpty(originalDescription)) {
                        Spanned descriptionText = Html.fromHtml(originalDescription);
                        textView.setText(descriptionText);
                    } else {
                        textView.setText("");
                    }

                    return true;
                }

                if (cursor.getColumnName(columnIndex).equals(Beers.COUNTRY_NAME)) {

                    String city = cursor.getString(cursor.getColumnIndex(Beers.CITY));
                    String country = cursor.getString(cursor.getColumnIndex(Beers.COUNTRY_NAME));


                    String cityCountry = "";
                    if (!TextUtils.isEmpty(city)) {
                        cityCountry += city;
                    }
                    if (!TextUtils.isEmpty(cityCountry) && !TextUtils.isEmpty(country)) {
                        cityCountry += ", " + country;
                    } else if (!TextUtils.isEmpty(country)) {
                        cityCountry += country;
                    }
                    TextView castView = (TextView) view;
                    castView.setText(cityCountry);

                    return true;

                }

                if (cursor.getColumnName(columnIndex).equals(Beers.CAN)) {

                    int can = cursor.getInt(cursor.getColumnIndex(Beers.CAN));
                    int draught = cursor.getInt(cursor.getColumnIndex(Beers.DRAUGHT));
                    int bottle = cursor.getInt(cursor.getColumnIndex(Beers.BOTTLE));
                    Boolean isCan = Util.intToBoolean(can, false);
                    Boolean isDraught = Util.intToBoolean(draught, false);
                    Boolean isBottled = Util.intToBoolean(bottle, false);

                    ImageView textBeerContainer = (ImageView) view;

                    if (isCan) {

                        textBeerContainer.setImageDrawable(canImage);
                    } else if (isDraught) {

                        textBeerContainer.setImageDrawable(draughtImage);
                    } else if (isBottled) {

                        textBeerContainer.setImageDrawable(bottleImage);
                    } else {
                        textBeerContainer.setImageDrawable(null);
                    }

                    return true;
                }


                //If bound, false otherwise
                return false;
            }
        });

    }


    public static final Intent makeDialog(Context cin, Integer beerId) {
        Intent intent = new Intent(cin, BeerDialog2.class);
        intent.putExtra(Beers.BEER_ID, beerId);
        return intent;
    }

    private OnClickListener dismissButtonListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            closeDialog();

        }

    };

    private void closeDialog() {
        this.setResult(Activity.RESULT_OK);
        this.finish();
    }


}
